#include "lightbeamdamager.h"

#include "explosion.h"
#include "terrain.h"
#include "player.h"

class CSnowman;

CLightBeamDamager::CLightBeamDamager()
{
	velocity = CVector(0.0, 0.0, 750.0);
	acceleration = CVector(0.0, 0.0, 0.0);
	distanceTravel = 0.0;
	size = 1.0f;
	isExplosion = false;
	usedUp = false;

	Load();
}

CLightBeamDamager::~CLightBeamDamager() {}

void CLightBeamDamager::OnAnimate(scalar_t deltaTime)
{
	//This snowball (previously rocket) distance calculation code is much more accurate.
	//distanceTravel no longer needs to be an absurdly large number. -SR
	float speed = velocity.z * deltaTime;

	CVector delta = CVector((float)cos(DEG2RAD(direction)),
							(float)sin(DEG2RAD(pitch)),
							(float)sin(DEG2RAD(direction))) * speed;
	position = position + delta;

	distanceTravel += delta.Length();

	if (!isExplosion)
	{
		if (distanceTravel >= 250.0f)
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
		}
	}
}

void CLightBeamDamager::OnCollision(CObject *collisionObject)
{
	if (!isExplosion)
	{
		if (typeid(*collisionObject) == typeid(CTerrain))
		{
			if (((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size >= position.y)
			{
				isExplosion = true;
				velocity = CVector(0.0, 0.0, 0.0);

				// Ian Bell - 12/5/2012
				CVector diff = player->position - position;
				// Get distance to player
				float distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);

				if(distFromPlayer < 200)
					if((clock() - start) / CLOCKS_PER_SEC > 1)// 1 second has elapsed
					{
						if(distFromPlayer < 75)
							audioSys->PlaySoundDX(audioSys->iLaserImpact);
						else // < 200
							audioSys->PlaySoundDX(audioSys->iLaserImpact_low);

						start = clock();
					}
			}
		
			// do explosion
		}
		if (typeid(*collisionObject) == typeid(CSnowman))
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);

			// Ian Bell - 12/5/2012
			CVector diff = player->position - position;
			// Get distance to player
			float distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);

			// Ian Bell - 12/5/2012
			if(distFromPlayer < 200)
				if((clock() - start) / CLOCKS_PER_SEC > 1)// 1 second has elapsed
				{
					if(distFromPlayer < 75)
						audioSys->PlaySoundDX(audioSys->iLaserImpact);
					else // < 200
						audioSys->PlaySoundDX(audioSys->iLaserImpact_low);

					start = clock();
				}
		}
	}
}

void CLightBeamDamager::OnDraw(CCamera *camera)
{
	// if the rocket has not yet exploded, draw the rocket model
	if (!isExplosion)
	{
		glEnable(GL_TEXTURE_2D);
		glColor3f(1.0, 1.0, 1.0);
		glTranslatef(position.x, position.y, position.z);
		glRotatef(-direction, 0.0, 1.0, 0.0);
		glScalef(0.025f, 0.025f, 0.025f);
		RenderFrame(0);
		glDisable(GL_TEXTURE_2D);
	}
}


void CLightBeamDamager::Load()
{
	CMD2Model::Load("..\\..\\res\\models\\rocketair.md2", "..\\..\\res\\models\\rocket.pcx");
}

void CLightBeamDamager::Unload()
{
}

void CLightBeamDamager::OnPrepare()
{
	// perform collision detection from this entity with all other objects in world
	if (!isExplosion)
		ProcessCollisions(FindRoot());
	if (isExplosion)
		isDead = true;
}

clock_t CLightBeamDamager::start = clock();